//
//  NRWCarInspectionCell.m
//  New_Runway
//
//  Created by 邢毅 on 2018/4/20.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarInspectionCell.h"

@implementation NRWCarInspectionCell
{
    UIImageView *_carImageView;
    UIImageView *_carVerbImageView;
    UIImageView *_carScheduleImageView;
    
    UILabel *_carTitleLabel;
    UILabel *_detailLabel;
    UILabel *_vinLabel;
    UILabel *_timeLabel;
    
    UIButton *_checkBtn;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupUI];
        
        [self layoutUI];
    }
    return self;
}

- (void)setupUI {
    
    _carImageView = DPImageView().setBackgroundColor(kColorWhite());
    _carVerbImageView = DPImageView().setImage(getImageFromName(@"mark-a"));
    _carScheduleImageView = DPImageView().setImage(getImageFromName(@"mark-b"));
    
    _carTitleLabel = DPLabel()
    .setFont(kPingFangRegular13())
    .setTextColor(getColorFromString(@"#323232"))
    .setBackgroundColor(kColorWhite());
    _carTitleLabel.numberOfLines = 2;
    
    _detailLabel = DPLabel()
    .setFont(kPingFangRegular11())
    .setTextColor(getColorFromString(@"#989898"))
    .setBackgroundColor(kColorWhite());
    
    _vinLabel = DPLabel()
    .setFont(kPingFangRegular11())
    .setTextColor(getColorFromString(@"#989898"))
    .setBackgroundColor(kColorWhite());
    
    _timeLabel = DPLabel()
    .setFont(kPingFangRegular11())
    .setTextColor(getColorFromString(@"#989898"))
    .setBackgroundColor(kColorWhite());
    
    _checkBtn = DPButton().addClickAction(self, @selector(checkBtnDidClick:))
    .setTitleFont(kPingFangRegular14());
    
    self.contentView
    .addSubview(_carImageView)
    .addSubview(_carTitleLabel)
    .addSubview(_detailLabel)
    .addSubview(_vinLabel)
    .addSubview(_timeLabel)
    .addSubview(_checkBtn);
    
    _carImageView
    .addSubview(_carVerbImageView)
    .addSubview(_carScheduleImageView);
    
    _checkBtn.setNormalTitle(@"检测");
}

- (void)layoutUI {
    
    _carImageView.frame = CGRectMake(17, 15, 120, 85);
    _carVerbImageView.frame = CGRectMake(-4, -3, 55, 14);
    _carScheduleImageView.frame = CGRectMake(_carImageView.viewWidth - 37, 0, 37, 37);
    
    [_carTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_carImageView.mas_top);
        make.left.equalTo(_carImageView.mas_right).offset(9);
        make.right.equalTo(self.contentView.mas_right).offset(-11);
    }];
    
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_vinLabel.mas_top).offset(-16);
        make.left.equalTo(_carTitleLabel.mas_left);
        make.right.equalTo(_checkBtn.mas_left).offset(-8);
        make.height.equalTo(@11);
    }];
    
    [_vinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_carImageView.mas_bottom);
        make.left.equalTo(_detailLabel.mas_left);
        make.right.lessThanOrEqualTo(_timeLabel.mas_left).offset(-8);
        make.height.equalTo(@11);
    }];
    
    [_checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@30);
        make.width.equalTo(@70);
        make.bottom.equalTo(_timeLabel.mas_top).offset(-7);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_vinLabel.mas_bottom);
        make.right.equalTo(_checkBtn.mas_right);
        make.height.equalTo(@11);
    }];
}

- (void)checkBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(carInspectionCell:checkBtnDidClick:)]) {
        
        [self.delegate carInspectionCell:self checkBtnDidClick:_checkBtn];
    }
}

- (void)setCarTitle:(NSString *)carTitle {
    
    _carTitle = carTitle;
    
    _carTitleLabel.text = carTitle;
}

- (void)setDetailString:(NSString *)detailString {
    
    _detailString = detailString;
    
    _detailLabel.text = detailString;
}

- (void)setCarVinString:(NSString *)carVinString {
    
    _carVinString = carVinString;
    
    _vinLabel.text = carVinString;
}

- (void)setTimeString:(NSString *)timeString {
    
    _timeString = timeString;
    
    _timeLabel.text = timeString;
}

- (void)setCarImage:(NSString *)carImage {
    
    _carImage = carImage;
    
    [_carImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", ImageApi, carImage]] placeholderImage:getImageFromName(@"tu")];
}

- (void)setCarOrderState:(NSInteger)carOrderState {
    
    _carScheduleImageView.hidden = !carOrderState;
}

- (void)setCarAuthState:(NSInteger)carAuthState {
    
   _carVerbImageView.hidden = !carAuthState;
}
//500 未初检
//510 初检不通过
//520 初检通过
//530 申请复检中
//540 复检完成

- (void)setCarDetectState:(NSInteger)carDetectState andCarStockState:(NSInteger)carStockState {
    
    if (carDetectState == 500) {//500 未初检
        if (carStockState == 40) {//销售出库
            [self setCheckBtnNotChecked];
        } else {
            [self setCheckBtnCanCheck];
        }
    } else if (carDetectState == 510) {//510 初检不通过
        [self setCheckBtnCheckFailed];
    } else if (carDetectState == 520) {//520 初检通过
        [self setCheckBtnCheckPass];
    } else if (carDetectState == 530) {//530 申请复检中
        [self setCheckBtnRetestting];
    } else if (carDetectState == 540) {//540 复检完成
        [self setCheckBtnRetestCompleted];
    }
}

- (void)setCheckBtnCanCheck {
    
    _checkBtn.setBackgroundColor(getColorFromString(@"#FF6816"), UIControlStateNormal)
    .setNormalTitleColor(kColorWhite())
    .setNormalTitle(@"检测");;
    _checkBtn.userInteractionEnabled = YES;
    _checkBtn.layer.cornerRadius = 5;
}

- (void)setCheckBtnNotChecked {
    
    _checkBtn.setBackgroundColor(kColorWhite(), UIControlStateNormal)
    .setNormalTitleColor(getColorFromString(@"#FFAA07"))
    .setNormalTitle(@"未初检");;
    _checkBtn.layer.cornerRadius = 0;
    _checkBtn.userInteractionEnabled = NO;
}

- (void)setCheckBtnCheckFailed {
    
    _checkBtn.setBackgroundColor(kColorWhite(), UIControlStateNormal)
    .setNormalTitleColor(getColorFromString(@"#FF4949"))
    .setNormalTitle(@"初检未通过");
    _checkBtn.layer.cornerRadius = 0;
    _checkBtn.userInteractionEnabled = NO;
}

- (void)setCheckBtnCheckPass {
    
    _checkBtn.setBackgroundColor(kColorWhite(), UIControlStateNormal)
    .setNormalTitleColor(getColorFromString(@"#00B90D"))
    .setNormalTitle(@"初检通过");
    _checkBtn.layer.cornerRadius = 0;
    _checkBtn.userInteractionEnabled = NO;
}

- (void)setCheckBtnRetestting {
    
    _checkBtn.setBackgroundColor(kColorWhite(), UIControlStateNormal)
    .setNormalTitleColor(getColorFromString(@"#FFAA07"))
    .setNormalTitle(@"申请复检中");
    _checkBtn.layer.cornerRadius = 0;
    _checkBtn.userInteractionEnabled = NO;
}

- (void)setCheckBtnRetestCompleted {
    
    _checkBtn.setBackgroundColor(kColorWhite(), UIControlStateNormal)
    .setNormalTitleColor(getColorFromString(@"#00B90D"))
    .setNormalTitle(@"复检完成");
    _checkBtn.layer.cornerRadius = 0;
    _checkBtn.userInteractionEnabled = NO;
}

- (void)setTag:(NSInteger)tag {
    
    [super setTag:tag];
    
    _checkBtn.tag = tag;
}

@end
